
// 获取mongoose
// const { query } = require("express");
const mongoose = require("mongoose");
// const router = require("../routes/address");

//get 列表数据
const get = async ({ curPage, eachPage }) => {
    // 强制转换类型 number转string
    (curPage = ~~curPage), (eachPage = ~~eachPage);


    const data = { total: 0, maxPage: 0, rows: [] }

    //mongoose.model传三个参数是创建  传一个参数是获取
    const selfMention = mongoose.model("selfMention");

    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await selfMention.countDocuments();

    // Math.ceil往上取整
    data.maxPage = Math.ceil(data.total / eachPage);
    // find查询所有数据 findOne查询满意结果的第一条
    // populate("gradle") 填充涉及到数据
    // skip跳过多少条开始查找 
    //limit 查询多少条数据
    // sort 排序
    //execute 立即执行
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await selfMention.find().skip(skipNum).limit(eachPage).sort({ _id: -1 }).exec();
    return data;

};
// 新增驿站
const add = async ({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance }) => {
    const selfMention = mongoose.model('selfMention');
    return await selfMention.create({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance })
}

// 删除数据
const Delete = async _id => {
    const selfMention = mongoose.model("selfMention");
    await selfMention.remove({ _id })
    const data = { status: true };
    return data
}
// 修改数据
const amend = async ({ _id, name, address, phone, Open_hours, longitude, latitude }) => {
    const selfMention = mongoose.model('selfMention');
    // updateOne更新符合条件的第一个文档 参数一查询条件 参数二需要修改的数据
    await selfMention.findOneAndUpdate({ _id }, { name, address, phone, Open_hours, longitude, latitude })
    const data = { status: true };
    return data;
}

// 查询一条数据
const getOne = async _id => {
    const selfMention = mongoose.model("selfMention");
    // 通过数据库_id拿到一条数据
    return await selfMention.findOne({ _id });
}

// 搜索数据
const getsearch = async name => {
    const selfMention = mongoose.model("selfMention");
    // find的方法查询条件必须是个对象  new RegExp(name)模糊查询
    const data = await selfMention.find({ name: new RegExp(name) })
    return data
}

// 向数据库对应id添加图片 param需要修改的数据 上传图片 query查询条件
const upData = async (query, param) => {
    const selfMention = mongoose.model("selfMention");
    // updateOne更新符合条件的第一个文档
    return await selfMention.updateOne(query, param)
}
// 删除数据库对应id的图片
const delImg = async ({ _id, url }) => {
    const selfMention = mongoose.model("selfMention");
    return await selfMention.findOneAndDelete({ _id }, { imgs: url })
}
//uniapp 新增一条并别删除其他数据
const addAddr = async ({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance }) => {
    const selfMention = mongoose.model('selfMention');
    // 查询所有数据
    const data = await selfMention.find();
    if (data) {
        // 如果有就先删除
        await selfMention.remove();
        return await selfMention.create({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance })
    }
    // 如果没有 就直接新增
    return await selfMention.create({ name, address, phone, Open_hours, longitude, latitude, imgs, _distance })


}
// uniapp 获取当前提货点
const getAddr = async () => {
    const selfMention = mongoose.model("selfMention");
    // 通过数据库_id拿到一条数据
    return await selfMention.find();
}

module.exports = { get, add, amend, getOne, getsearch, upData, delImg, Delete, addAddr ,getAddr}
